#include "propertyfilter.h"
#include <QDebug>

PropertyFilter::PropertyFilter()
{
}

int PropertyFilter::add(const Url &property, PropertyFilter::PROPERTY_FILTER_TYPE filterType, QObject *reciever, const char *slotOrPropertyName)
{
    int idx = m_propertyFilters.size();
    m_propertyFilters.insert(idx, {property,
                                   filterType,
                                   reciever,
                                   slotOrPropertyName});
    return idx;
}

void PropertyFilter::remove(int filterIdx)
{
    m_propertyFilters.remove(filterIdx);
}

void PropertyFilter::filter(const Url &propertyUrl)
{
    QHashIterator<int, property_filter_t> it(m_propertyFilters);
    while (it.hasNext()) {
        it.next();
        const property_filter_t &filter = it.value();
        if (filter.properties.match(propertyUrl)) {
            qDebug() << ">>>>> property filtered" << filter.methodOrProperty << propertyUrl.argumentData();
            if (filter.type == PROPERTY_SET)
                filter.object->setProperty(filter.methodOrProperty,
                                           propertyUrl.argumentData());
            else // slot call
                QMetaObject::invokeMethod(filter.object, filter.methodOrProperty,
                                          Q_ARG(QVariant, propertyUrl.argumentData()));
        }
    }
}
